<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Admin\Logic;

/**
 * 支付接口逻辑类
 */
class PayLogic{
    public function tixian($uid){

        $one = D('Gather')->where(['uid' => $uid])->find();
        if ($one['frozen'] == 0) {
            echo "此账户已冻结，不可以提现！";
            die;
        }
        $yh = D('Ptyh')->where(['uid' => $uid])->find();
        $alipay = $yh['alipay'];
//        $amount = $one['money'];
        $amount = ($one['yongjin']*100 + $one['fenrun']*100)/100;

        if($amount < 0) {
            return;
        }
        $out_biz_no = date("YmdHis") . rand(100000, 999999);
        $one = D('Ptyh')->where('uid=' . $uid)->find();
        $payee_real_name = $one['name'];
        $payer_show_name = '绿叶收银台';
        $time = time();

        // 判断结束
        $extract = array(
            'uid' => $uid,
            'money' => ($amount*100-100)/100,//存入的是扣除手续费的金额
            'time' => time(),
            'status' => 0,
            'out_biz_no' => $out_biz_no,
        );

        // print_r($extract);die;
        $id = D('Extract')->add($extract);

        // 调用预下单接口得到付款二维码
        Vendor('dangmianfu_demo_php.f2fpay.qrpay_test');
        $transfer = new \Alipay;

        $adata = D('Adata')->where(['id' => 1])->find();
        // $transfer->app_auth_token=$app_auth_token;//口碑商家授权令牌（代收款）!!!!!!!!!!!!!!!!该参数是公共参数！！！！！！！！！！！！
        $remark = '商户提现';
        $_amount = ($amount*100-100)/100;;//实际到款


        // 查看当日提现总金额  超过95万则使用另一个支付宝
        $time1 = strtotime(date("Ymd", strtotime("+1 day")));
        $time2 = strtotime(date("Ymd"));
        $td_money = D('Dingdan')->where("status=1 and pay_time between $time2 and $time1")->sum('pay_money');

        $result = $transfer->transfer8($out_biz_no, $alipay, $_amount, $remark, $payee_real_name, $payer_show_name, $adata);


        if (empty($result->sub_msg) && $result->out_biz_no == $out_biz_no) {
            // 说明是成功的   1、提现状态修改 2、提现次数、用户余额修改

            D('Gather')->where('uid=' . $uid)->save(['yongjin' => 0,'fenrun' => 0]);


            $data = [
                'name' => $payee_real_name,
                'money' => $amount,
                'remark' => '提现单号：' . $out_biz_no . '，如有疑问请联系客服进行咨询。',
            ];

            $member = D('Ptyh')->where(['uid' => $uid])->find();
            file_put_contents("hb.txt", date('Y-m-d H:i:s') . ' 一建提现成功: ' . json_encode($data) . PHP_EOL, FILE_APPEND);

            return ['code' => 1, 'msg' => '提现成功','data' => ''];

        } else {
            file_put_contents("hb.txt", date('Y-m-d H:i:s') . ' 提现失败: ' . $result->sub_msg . PHP_EOL, FILE_APPEND);

            return ['code' => 1, 'msg' => '提现失败','data' =>  $result->sub_msg];
        }
    }
}